/**
 * @param {number[]} arr
 * @return {number[]}
 */
 var getDistances = function(arr) {
  const map = {}, sum = {}
  for(let i = 0;i<arr.length; i++){
    map[arr[i]] = map[arr[i]] || []
    sum[arr[i]] = sum[arr[i]] || [0]
    map[arr[i]].push(i)
    let x = sum[arr[i]][sum[arr[i]].length-1]
    sum[arr[i]].push(i + x)
  }
  // target >= 0
  const calc = (same, target, sum) => {
    let l = 0, r = same.length-1
    while(l < r){
      let m = (l + r) >> 1
      if(same[m] >= target){
        r = m
      }else{
        l = m + 1
      }
    }
    let suff = sum[sum.length-1] - sum[l] - (sum.length-1-l)*target
    let pre = l*target - sum[l]
    return suff + pre
  }
  let ans = []
  for(let i = 0; i<arr.length; i++){
    let same = map[arr[i]]
    let now = calc(same, i, sum[arr[i]])
    ans.push(now)
  }
  return ans
};